package cn.itliuliu.org.controller;

import cn.itliuliu.org.service.IShopService;
import cn.itliuliu.org.domain.Shop;
import cn.itliuliu.org.query.ShopQuery;
import cn.itliuliu.basic.util.AjaxResult;
import cn.itliuliu.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 保存和修改公用的
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        if ( shop.getId() != null){
            shopService.update(shop);
        }else{
            shopService.add(shop);
        }
        return AjaxResult.me();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        shopService.delete(id);
        return AjaxResult.me();
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.queryOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list() {
        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settle")
    public AjaxResult settle(@RequestBody @Valid Shop shop){
        shopService.settle(shop);
        return AjaxResult.me();
    }
}
